const tencentcloud = require('tencentcloud-sdk-nodejs-iai')
const IaiClient = tencentcloud.iai.v20200303.Client
const clientConfig = {
  // 腾讯云认证信息
  credential: {
    secretId: 'AKID7A0yhyfkFUWJqaBdmeHJkvpTsyCAgkgP',
    secretKey: 'ynLr9BObHgqumPq56cuwi04rRlpeCc1B',
  },
  // 产品地域
  region: 'ap-shanghai',
  // 可选配置实例
  profile: {
    signMethod: 'TC3-HMAC-SHA256', // 签名方法
    httpProfile: {
      reqMethod: 'POST', // 请求方法
      reqTimeout: 30, // 请求超时时间，默认60s
      endpoint: 'iai.tencentcloudapi.com',
    },
  },
}
class TencentFaceApi {
  // 查询用户组
  async faceGroupList() {
    try {
      const client = new IaiClient(clientConfig)
      const result = await client.GetGroupList({})
      return result
    } catch (error) {
      console.log('faceGroupList', error)
    }
  }
  // 查询用户列表
  async facePersonList() {
    try {
      const client = new IaiClient(clientConfig)
      const result = await client.GetPersonList({ GroupId: 'group1',Limit:999 })
      return result
    } catch (error) {
      console.log('facePersonList', error)
    }
  }
  // 人脸注册
  async faceAddPerson(body) {
    try {
      const client = new IaiClient(clientConfig)
      const result = await client.CreatePerson({
        GroupId: 'group1',
        NeedRotateDetection: 1,
        UniquePersonControl: 4,
        ...body,
      })
      return result
    } catch (error) {
      console.log('faceAddPerson', error.message)
      return error.message
    }
  }
  // 人脸搜索
  async faceSearch(body) {
    try {
      const client = new IaiClient(clientConfig)
      const Integers = [
        'MaxFaceNum',
        'MinFaceSize',
        'MaxPersonNum',
        'NeedPersonInfo',
        'QualityControl',
        'NeedRotateDetection',
        'FaceMatchThreshold',
      ]
      Integers.forEach((item) => {
        if (body.hasOwnProperty(item)) {
          body[item] = parseInt(body[item])
        }
      })
      console.time('faceSearch')
      const result = await client.SearchFaces({
        GroupIds: ['group1'],
        ...body,
      })
      console.timeEnd('faceSearch')
      return result
    } catch (error) {
      console.log('faceSearch',error.message)
      return error.message
    }
  }
}

module.exports = new TencentFaceApi()
